package io.renren.modules.photo.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.oss.cloud.OSSFactory;
import io.renren.modules.oss.dto.UploadResultDTO;
import io.renren.modules.photo.dto.PhotoInfoDTO;
import io.renren.modules.photo.dto.PhotoInfoSaveDTO;
import io.renren.modules.photo.dto.PhotoInfoUpdateLabelDTO;
import io.renren.modules.photo.service.PhotoInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @Author huan.xu
 * @Date 2022/11/2 11:07
 * @Description
 */
@RestController
@RequestMapping("/photo/info")
@Api(tags = "照片列表")
public class PhotoInfoController {

    @Autowired
    private PhotoInfoService photoInfoService;

    @GetMapping("page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType = "int"),
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query", required = true, dataType = "int"),
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType = "String"),
        @ApiImplicitParam(name = "name", value = "角色名", paramType = "query", dataType = "String")
    })
    @RequiresPermissions("photo:info:page")
    public Result<PageData<PhotoInfoDTO>> page(
        @ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<PhotoInfoDTO> page = photoInfoService.page(params);

        return new Result<PageData<PhotoInfoDTO>>().ok(page);
    }

    @ApiOperation(value = "上传照片")
    @PostMapping("/upload")
    @RequiresPermissions("photo:info:upload")
    public Result upload(@RequestParam("file") MultipartFile file) throws IOException {
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        UploadResultDTO uploadResultDTO = OSSFactory.build().uploadSuffix(file.getBytes(), extension);
        return new Result().ok(uploadResultDTO);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    @RequiresPermissions("photo:info:save")
    public Result save(@RequestBody PhotoInfoSaveDTO photoInfoSaveDTO){
        //效验数据
        ValidatorUtils.validateEntity(photoInfoSaveDTO, AddGroup.class);

        photoInfoService.save(photoInfoSaveDTO);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改标签")
    @LogOperation("修改标签")
    @RequiresPermissions("photo:info:update:label")
    public Result updateLabel(@RequestBody PhotoInfoUpdateLabelDTO updateLabelDTO){
        //效验数据
        ValidatorUtils.validateEntity(updateLabelDTO, AddGroup.class);

        photoInfoService.updateLabel(updateLabelDTO);

        return new Result();
    }

    @DeleteMapping
    @ApiOperation("删除")
    @LogOperation("删除")
    @RequiresPermissions("photo:info:delete")
    public Result delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        photoInfoService.delete(ids);
        return new Result();
    }

    @GetMapping("get-label")
    @ApiOperation("组装下拉框数据")
    @RequiresPermissions("photo:info:getLabel")
    public Result showLabel(@RequestParam("infoId") Long infoId) {
        List<Map> selectOptions= photoInfoService.getPhotoLabelByInfoId(infoId);
        Map returnMap=new HashMap(16);
        returnMap.put("labelSelectOptions",selectOptions);
        return new Result().ok(returnMap);
    }
}
